import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import '../network/api_client.dart';

class LoginTestWidget extends StatefulWidget {
  @override
  _LoginTestWidgetState createState() => _LoginTestWidgetState();
}

class _LoginTestWidgetState extends State<LoginTestWidget> {
  final _apiClient = ApiClient();
  String _result = '';
  bool _isLoading = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Login Test')),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: [
            ElevatedButton(
              onPressed: _isLoading ? null : _testLogin,
              child: _isLoading ? CircularProgressIndicator() : Text('Test Login'),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _isLoading ? null : _testPing,
              child: Text('Test Ping'),
            ),
            SizedBox(height: 20),
            Expanded(
              child: SingleChildScrollView(
                child: Text(_result),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _testLogin() async {
    setState(() {
      _isLoading = true;
      _result = 'Testing login...';
    });

    try {
      final response = await _apiClient.login('admin', 'password');
      setState(() {
        _result = 'Login Success:\n${response.toString()}';
      });
    } catch (e) {
      setState(() {
        _result = 'Login Error:\n$e';
      });
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }

  void _testPing() async {
    setState(() {
      _isLoading = true;
      _result = 'Testing ping...';
    });

    try {
      final response = await _apiClient.ping();
      setState(() {
        _result = 'Ping Success:\n${response.toString()}';
      });
    } catch (e) {
      setState(() {
        _result = 'Ping Error:\n$e';
      });
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}

void main() {
  runApp(MaterialApp(
    home: LoginTestWidget(),
  ));
}
